package com.qdlc.p2p.biz.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.InterestService;
import com.qdlc.p2p.common.constant.BorrowStatus;
import com.qdlc.p2p.common.constant.BorrowStyle;
import com.qdlc.p2p.common.constant.BorrowTenderStatus;
import com.qdlc.p2p.common.constant.BorrowTimeType;
import com.qdlc.p2p.common.constant.VerifyLogType;
import com.qdlc.p2p.common.constant.VerifyType;
import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.dao.VerifyLogDao;
import com.qdlc.p2p.dal.dto.Borrow;
import com.qdlc.p2p.dal.dto.VerifyLog;
import com.qdlc.p2p.dal.model.BorrowModel;
import com.qdlc.p2p.dal.model.interest.InstallmentRepaymentCalculator;
import com.qdlc.p2p.dal.model.interest.InterestCalculator;
import com.qdlc.p2p.dal.model.interest.MonthlyInterestCalculator;
import com.qdlc.p2p.dal.model.interest.OnetimeRepaymentCalculator;

@Service("interestService")
public class InterestServiceImpl implements InterestService {

    @Resource
    private VerifyLogDao verifyLogDao;

    /**
     * 计算体验标利息
     *
     * @param borrowModel   标模型
     * @param tenderAddTime 投标时间
     * @return 利息实体
     */
    @Override
    public InterestCalculator experienceInterestCalculator(BorrowModel borrowModel, Date tenderAddTime) {
        double apr = borrowModel.getApr() / 100;
        InterestCalculator ic = new OnetimeRepaymentCalculator(borrowModel.getMoney(), apr, tenderAddTime, 1, 0);
        ic.calculator(borrowModel.getTimeLimit());
        return ic;
    }

    /**
     * 计算利息
     *
     * @param borrow 标
     * @param amount 金额
     * @return 利息实体
     */
    @Override
    public InterestCalculator interestCalculator(Borrow borrow, double amount) {

        boolean flag = (borrow.getStatus() == BorrowStatus.STATUS_PUBLISHING)
                || (borrow.getStatus() == BorrowStatus.STATUS_RECHECK_PASS)
                || (borrow.getIsTender() == BorrowTenderStatus.SUCCESS);

        double apr = borrow.getApr() / 100;
        Date date;
        VerifyLog verifyLog;
        long id = borrow.getId();
        if (flag) {
            verifyLog = verifyLogDao.findByType(id, VerifyLogType.BORROW, VerifyType.RECHECK); // 2:复审
        } else {
            verifyLog = verifyLogDao.findByType(id, VerifyLogType.BORROW, VerifyType.TRIAL); // 1:初审
        }

        if (null != verifyLog
                && null != verifyLog.getTime()) {
            date = verifyLog.getTime();
        } else {
            date = DateUtil.getNow();
        }

        // 利息管理费
        double manageFee = 0;
        // 期数
        int periods = borrow.getTimeLimit();
        // 利息实例
        InterestCalculator calculator;
        int borrowStyle = borrow.getStyle();
        int borrowTimeType = borrow.getBorrowTimeType();
        switch (borrowStyle) {
            case BorrowStyle.STYLE_ONETIME_REPAYMENT: // 一次性还款
                if (borrowTimeType == BorrowTimeType.DAY) { // 天标
                    periods = 1;
                }
                calculator = new OnetimeRepaymentCalculator(amount, apr, date, periods, manageFee);
                break;
            case BorrowStyle.STYLE_MONTHLY_INTEREST:// 每月还息到期还本
                calculator = new MonthlyInterestCalculator(amount, apr, date, periods, flag, manageFee);
                break;
            case BorrowStyle.STYLE_INSTALLMENT_REPAYMENT:// 等额本息
                calculator = new InstallmentRepaymentCalculator(amount, apr, date, periods, manageFee);
                break;
            default:
                throw new BusinessException("标还款类型不正确!", BusinessException.TYPE_JSON);
        }
        if (borrowTimeType == BorrowTimeType.DAY) { // 天标
            calculator.calculator(periods);
        } else {
            calculator.calculator();
        }

        return calculator;
    }

    @Override
    public InterestCalculator interestCalculator(int style, double amount, double apr, int borrowTimeType, int timeLimit) {
        // 利息实例
        InterestCalculator calculator;
        // 利息管理费
        double manageFee = 0;
        Date date = DateUtil.getNow();
        switch (style) {
            case BorrowStyle.STYLE_ONETIME_REPAYMENT: // 一次性还款
                if (borrowTimeType == BorrowTimeType.DAY) { // 天标
                    timeLimit = 1;
                }
                calculator = new OnetimeRepaymentCalculator(amount, apr, date, timeLimit, manageFee);
                break;
            case BorrowStyle.STYLE_MONTHLY_INTEREST:// 每月还息到期还本
                calculator = new MonthlyInterestCalculator(amount, apr, date, timeLimit, true, manageFee);
                break;
            case BorrowStyle.STYLE_INSTALLMENT_REPAYMENT:// 等额本息
                calculator = new InstallmentRepaymentCalculator(amount, apr, date, timeLimit, manageFee);
                break;
            default:
                throw new BusinessException("标还款类型不正确!", BusinessException.TYPE_JSON);
        }
        if (borrowTimeType == BorrowTimeType.DAY) { // 天标
            calculator.calculator(timeLimit);
        } else {
            calculator.calculator();
        }

        return calculator;
    }
}
